﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Beat_em_Up.Scenes
{
    class Button
    {
        Texture2D mouse_up, mouse_over, mouse_click;
        //Texture2D? mouse_click;
        public Rectangle obj_menu;
        Rectangle obj_mouse;
        
        //animacao

        enum StatusMouse { ON, OFF };
        StatusMouse statusMouse;

        public bool wasClicked;

        int clickNum;

        public Button(Texture2D mouse_up)//, Texture2D? _mouse_over, Texture2D? mouse_click)
        {
            this.mouse_up = mouse_up;

            obj_menu = new Rectangle();

            obj_menu.Width = mouse_up.Width;//LEVAR EM CONTA A ANIMACAO
            obj_menu.Height = mouse_up.Height;
            obj_menu.X = 100;//COLOCAR EM METODO SEPARADO
            obj_menu.Y = 100;

            wasClicked = false;
            statusMouse = StatusMouse.OFF;

            clickNum = 1;
            //this.mouse_over = 
            //this.mouse_click = mouse_click;
        }

        public Button(Texture2D mouse_up, Texture2D _mouse_over)
        {
            this.mouse_up = mouse_up;
            this.mouse_over = _mouse_over;

            obj_menu.Width = mouse_up.Width;//LEVAR EM CONTA A ANIMACAO
            obj_menu.Height = mouse_up.Height;

            wasClicked = false;
            statusMouse = StatusMouse.OFF;
            clickNum = 1;
        }

        public Button(Texture2D mouse_up, Texture2D mouse_over, Texture2D mouse_click)
        {
            this.mouse_up = mouse_up;
            this.mouse_over = mouse_over;
            this.mouse_click = mouse_click;

            obj_menu.Width = mouse_up.Width;//LEVAR EM CONTA A ANIMACAO
            obj_menu.Height = mouse_up.Height;

            wasClicked = false;
            statusMouse = StatusMouse.OFF;
            clickNum = 1;
        }

        public void checkMouseOver (MouseState mouse)
        {
            obj_mouse.X = mouse.X;
            obj_mouse.Y = mouse.Y;
            obj_mouse.Width = 32;
            obj_mouse.Height = 32;

            if (obj_mouse.Intersects(obj_menu))
            {
                statusMouse = StatusMouse.ON;
            }

            else
            {
                statusMouse = StatusMouse.OFF;
            }
        }

        public bool checkMouseClick (MouseState mouse)
        {
            obj_mouse.X = mouse.X;
            obj_mouse.Y = mouse.Y;
            obj_mouse.Width = 32;
            obj_mouse.Height = 32;

            if (mouse.LeftButton == ButtonState.Pressed)
            {
                if (obj_menu.Contains(mouse.X, mouse.Y))
                {
                    wasClicked = true;
                    clickNum++;
                    Console.WriteLine("clique" + clickNum);
                    return true;
                }
                else
                {
                    return false;
                }
            }

            else
            {
                wasClicked = false;
                return false;
            }
        }

        public void Update(GameTime gameTime, MouseState mouse)
        {
            checkMouseOver(mouse);
            checkMouseClick(mouse);
        }
        
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {   
            //spriteBatch.Draw(mouse_up, obj_menu, Color.White);
            

            if (wasClicked == true)
            {
                spriteBatch.Draw(mouse_click, obj_menu, Color.White);
            }

            else if (statusMouse == StatusMouse.ON)
            {
                spriteBatch.Draw(mouse_over, obj_menu, Color.White);
            }
            //else if (wasClicked == true)
            //{
            //    spriteBatch.Draw(mouse_click, obj_menu, Color.White);
            //}

            else
            {
                spriteBatch.Draw(mouse_up, obj_menu, Color.White);
            }

            //spriteBatch.Draw(mouse_up, Vector2.Zero, Color.White);
            //if (mouse_over != null)
            //{
            //    spriteBatch.Draw(mouse_over, posicao, Color.White);
            //}

            //if (mouse_click != null)
            //{
            //    spriteBatch.Draw(mouse_click, Vector2.Zero, Color.White);
            //}
        }
    }
}

